home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLINC.PAK / VALIDATE.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  10KB  |  408 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.4  $
  6. //
  7. //----------------------------------------------------------------------------
  8. #if !defined(OWL_VALIDATE_H)
  9. #define OWL_VALIDATE_H
  10.  
  11. #if !defined(OWL_WINDOW_H)
  12. # include <owl/window.h>
  13. #endif
  14. #if !defined(CLASSLIB_ARRAYS_H)
  15. # include <classlib/arrays.h>
  16. #endif
  17. #if !defined(OWL_BITSET_H)
  18. # include <owl/bitset.h>
  19. #endif
  20. #include <owl/validate.rh>
  21.  
  22. #if defined(BI_NAMESPACE)
  23. namespace OWL {
  24. #endif
  25.  
  26. // Generic definitions/compiler options (eg. alignment) preceeding the 
  27. // definition of classes
  28. #include <services/preclass.h>
  29.  
  30. class _OWLCLASS_RTL TXValidator;
  31.  
  32. //
  33. // Validator option flags
  34. //
  35. enum TValidatorOptions {
  36.   voFill     =  0x0001,   // option to fill in chars on IsValidInput checks
  37.   voTransfer =  0x0002,   // option to perform conversion & transfer
  38.   voOnAppend =  0x0004,   // option to only validate input on appending
  39.   voReserved =  0x00F8    // reserved for future use
  40. };
  41.  
  42. //
  43. // class TValidator
  44. // ~~~~~ ~~~~~~~~~~
  45. class _OWLCLASS TValidator : public TStreamableBase {
  46.   public:
  47.     TValidator();
  48.     virtual ~TValidator();
  49.  
  50.     virtual void Error(TWindow* owner);  
  51.     virtual bool IsValidInput(char far* input, bool suppressFill);
  52.     virtual bool IsValid(const char far* input);
  53.     virtual uint Transfer(char far* text, void* buffer, TTransferDirection direction);
  54.     virtual int  Adjust(string& text, uint& begPos, uint& endPos, int amount); 
  55.  
  56.     // Checks input against validator for completeness. Never modifies input.
  57.     // Calls error if not valid.
  58.     //
  59.     bool         Valid(const char far* str, TWindow* owner = 0);
  60.     bool         HasOption(int option);
  61.     void         SetOption(int option);
  62.     void         UnsetOption(int option);
  63.  
  64. #if defined(OWL2_COMPAT)
  65.     typedef ::TXValidator TXValidator;  // Exception classes moved to global scope
  66. #endif
  67.  
  68.   protected:
  69.     uint16   Options;
  70.  
  71.   DECLARE_STREAMABLE(_OWLCLASS, TValidator, 1);
  72. };
  73.  
  74. //
  75. // class TXValidator
  76. // ~~~~~ ~~~~~~~~~~~
  77. class _OWLCLASS_RTL TXValidator : public TXOwl {
  78.   public:
  79.     TXValidator(uint resId = IDS_VALIDATORSYNTAX);
  80.  
  81. #if defined(BI_NO_COVAR_RET)
  82.     TXBase* Clone();
  83. #else
  84.     TXValidator* Clone();
  85. #endif
  86.     void Throw();
  87.  
  88.     static void Raise();
  89. };
  90.  
  91. //
  92. // enum TPXPictureValidator
  93. // ~~~~ ~~~~~~~~~~~~~~~~~~~
  94. // Validation result type
  95. //
  96. enum TPicResult {
  97.   prComplete,
  98.   prIncomplete,
  99.   prEmpty,
  100.   prError,
  101.   prSyntax,
  102.   prAmbiguous,
  103.   prIncompNoFill
  104. };
  105.  
  106. //
  107. // class TPXPictureValidator
  108. // ~~~~~ ~~~~~~~~~~~~~~~~~~~
  109. class _OWLCLASS TPXPictureValidator : public TValidator {
  110.   public:
  111.     TPXPictureValidator(const char far* pic, bool autoFill=false);
  112.  
  113.     // Override TValidator's virtuals
  114.     //
  115.     void         Error(TWindow* owner);
  116.     bool         IsValidInput(char far* str, bool suppressFill);
  117.     bool         IsValid(const char far* str);
  118.     int          Adjust(string& text, uint& begPos, uint& endPos, int amount); 
  119.  
  120.     virtual TPicResult Picture(char far* input, bool autoFill=false);
  121.  
  122.   protected:
  123.     const string& GetPic() const;
  124.     void          SetPic(const string& pic);
  125.  
  126.   protected_data:
  127.     string       Pic;
  128.  
  129.   private:
  130.     bool         IsComplete(TPicResult rslt);
  131.     bool         IsIncomplete(TPicResult rslt);
  132.     void         ToGroupEnd(uint termCh, uint& i);
  133.     bool         SkipToComma(uint termCh, uint& i);
  134.     uint         CalcTerm(uint termCh, uint i);
  135.     TPicResult   Iteration(char far* input, uint termCh, uint& i, uint& j);
  136.     TPicResult   Group(char far* input, uint termCh, uint& i, uint& j);
  137.     TPicResult   CheckComplete(uint termCh, uint& i, TPicResult rslt);
  138.  
  139.     TPicResult   Scan(char far* input, uint termCh, uint& i, uint& j);
  140.     TPicResult   Process(char far* input, uint termCh, uint& i, uint& j);
  141.     bool         SyntaxCheck();
  142.  
  143.   DECLARE_STREAMABLE(_OWLCLASS, TPXPictureValidator, 1);
  144. };
  145.  
  146. //
  147. // class TFilterValidator
  148. // ~~~~~ ~~~~~~~~~~~~~~~~
  149. class _OWLCLASS TFilterValidator : public TValidator {
  150.   public:
  151.     TFilterValidator(const TCharSet& validChars);
  152.  
  153.     // Override TValidator's virtuals
  154.     //
  155.     void         Error(TWindow* owner);
  156.     bool         IsValid(const char far* str);
  157.     bool         IsValidInput(char far* str, bool suppressFill);
  158.  
  159.   protected:
  160.     const TCharSet&  GetValidChars();
  161.     void             SetValidChars(const TCharSet& vc);
  162.  
  163.   protected_data:
  164.     TCharSet     ValidChars;
  165.  
  166.   DECLARE_STREAMABLE(_OWLCLASS, TFilterValidator, 1);
  167. };
  168.  
  169. //
  170. // class TRangeValidator
  171. // ~~~~~ ~~~~~~~~~~~~~~~
  172. class _OWLCLASS TRangeValidator : public TFilterValidator {
  173.   public:
  174.     TRangeValidator(long min, long max);
  175.  
  176.     // Override TValidator's virtuals
  177.     //
  178.     void         Error(TWindow* owner);
  179.     bool         IsValid(const char far* str);
  180.     uint         Transfer(char far* str, void* buffer, TTransferDirection direction);
  181.     int          Adjust(string& text, uint& begPos, uint& endPos, int amount); 
  182.  
  183.   protected:
  184.     long   GetMin();
  185.     void   SetMin(long min);
  186.     long   GetMax();
  187.     void   SetMax(long max);
  188.  
  189.   protected_data:
  190.     long   Min;
  191.     long   Max;
  192.  
  193.   DECLARE_STREAMABLE(_OWLCLASS, TRangeValidator, 1);
  194. };
  195.  
  196. //
  197. // class TLookupValidator
  198. // ~~~~~ ~~~~~~~~~~~~~~~~
  199. class _OWLCLASS TLookupValidator : public TValidator {
  200.   public:
  201.     TLookupValidator();
  202.  
  203.     // Override TValidator's virtuals
  204.     //
  205.     bool         IsValid(const char far* str);
  206.  
  207.     // Virtual lookup of a string
  208.     //
  209.     virtual bool Lookup(const char far* str);
  210.  
  211.   DECLARE_STREAMABLE(_OWLCLASS, TLookupValidator, 1);
  212. };
  213.  
  214. //
  215. // class TSortedStringArray
  216. // ~~~~~ ~~~~~~~~~~~~~~~~~~
  217. class _OWLCLASS TSortedStringArray {
  218.   public:
  219.     typedef void (*IterFunc)(string&, void*);
  220.     typedef int  (*CondFunc)(const string&, void*);
  221.  
  222.     TSortedStringArray(int upper, int lower, int delta);
  223.     int LowerBound() const;
  224.     int UpperBound() const;
  225.     uint ArraySize() const;
  226.     int IsFull() const;
  227.     int IsEmpty() const;
  228.     uint GetItemsInContainer() const;
  229.     int Add(const string& t);
  230.     int Detach(const string& t);
  231.     int Detach(int loc);
  232.     int Destroy(const string& t);
  233.     int Destroy(int loc);
  234.     int HasMember(const string& t) const;
  235.     int Find(const string& t) const;
  236.     string& operator [](int loc);
  237.     string& operator [](int loc) const;
  238.     void ForEach(IterFunc iter, void* args);
  239.     string* FirstThat(CondFunc cond, void* args) const;
  240.     string* LastThat(CondFunc cond, void* args) const;
  241.     void Flush();
  242.  
  243.   private:
  244.     TSArrayAsVector<string> Data;
  245.  
  246.   friend class TSortedStringArrayIterator;
  247. };
  248.  
  249. //
  250. // class TStringLookupValidator
  251. // ~~~~~ ~~~~~~~~~~~~~~~~~~~~~~
  252. class _OWLCLASS TStringLookupValidator : public TLookupValidator {
  253.   public:
  254.     TStringLookupValidator(TSortedStringArray* strings);
  255.    ~TStringLookupValidator();
  256.  
  257.     // Override TValidator's virtuals
  258.     //
  259.     void         Error(TWindow* owner);
  260.     int          Adjust(string& text, uint& begPos, uint& endPos, int amount); 
  261.  
  262.     // Override TLookupValidator's virtuals
  263.     //
  264.     bool         Lookup(const char far* str);
  265.  
  266.     void         NewStringList(TSortedStringArray* strings);
  267.  
  268.   protected:
  269.     const TSortedStringArray* GetStrings() const;
  270.     void                      SetStrings(TSortedStringArray* strings);
  271.  
  272.   protected_data:
  273.     TSortedStringArray* Strings;
  274.  
  275.   DECLARE_STREAMABLE(_OWLCLASS, TStringLookupValidator, 1);
  276. };
  277.  
  278. // Generic definitions/compiler options (eg. alignment) following the 
  279. // definition of classes
  280. #include <services/posclass.h>
  281.  
  282. #if defined(BI_NAMESPACE)
  283. } // namespace OWL
  284. #endif
  285.  
  286. //----------------------------------------------------------------------------
  287. // Inline Implementation
  288. //
  289.  
  290. //
  291. // Return true if the entry for the validator is valid.
  292. //
  293. inline bool TValidator::Valid(const char far* str, TWindow* owner)
  294. {
  295.   if (!IsValid(str)) {
  296.     Error(owner);
  297.     return false;
  298.   }
  299.   return true;
  300. }
  301.  
  302. //
  303. // Return true if the validator has a particular option.
  304. //
  305. inline bool TValidator::HasOption(int option)
  306. {
  307.   return Options & option;
  308. }
  309.  
  310. //
  311. // Turn on a validator option.
  312. //
  313. inline void TValidator::SetOption(int option)
  314. {
  315.   Options |= uint16(option);
  316. }
  317.  
  318. //
  319. // Turn off a validator option.
  320. //
  321. inline void TValidator::UnsetOption(int option)
  322. {
  323.   Options &= uint16(~option);
  324. }
  325.  
  326. //
  327. // Return the picture mask used by the validator.
  328. //
  329. inline const string& TPXPictureValidator::GetPic() const
  330. {
  331.   return Pic;
  332. }
  333.  
  334. //
  335. // Set the picture mask for the validator.
  336. //
  337. inline void TPXPictureValidator::SetPic(const string& pic)
  338. {
  339.   Pic = pic;
  340. }
  341.  
  342. //
  343. // Return the valid character set for the validator.
  344. //
  345. inline const TCharSet& TFilterValidator::GetValidChars()
  346. {
  347.   return ValidChars;
  348. }
  349.  
  350. //
  351. // Set the valid character set for the validator.
  352. //
  353. inline void TFilterValidator::SetValidChars(const TCharSet& vc)
  354. {
  355.   ValidChars = vc;
  356. }
  357.  
  358. //
  359. // Return the minimum number the validator can accept.
  360. //
  361. inline long TRangeValidator::GetMin()
  362. {
  363.   return Min;
  364. }
  365.  
  366. //
  367. // Set the minimum number the validator can accept.
  368. //
  369. inline void TRangeValidator::SetMin(long min)
  370. {
  371.   Min = min;
  372. }
  373.  
  374. //
  375. // Return the maximum number the validator can accept.
  376. //
  377. inline long TRangeValidator::GetMax()
  378. {
  379.   return Max;
  380. }
  381.  
  382. //
  383. // Set the maximum number the validator can accept.
  384. //
  385. inline void TRangeValidator::SetMax(long max)
  386. {
  387.   Max = max;
  388. }
  389.  
  390. //
  391. // Return the set of valid strings used by the validator.
  392. //
  393. inline const TSortedStringArray* TStringLookupValidator::GetStrings() const
  394. {
  395.   return Strings;
  396. }
  397.  
  398. //
  399. // Set the valid strings used by the validator.
  400. //
  401. inline void TStringLookupValidator::SetStrings(TSortedStringArray* strings)
  402. {
  403.   delete Strings;
  404.   Strings = strings;
  405. }
  406.  
  407. #endif  // OWL_VALIDATE_H
  408.